/**
 * Created by lesonli on 2016/11/17.
 */
import * as types from '../types'
import api from '../../api/modules/config'
import {
    get_list,
    search_user,
    get_teachers,
    get_student_managers,
    create_test_user
} from '../../api/modules/tools_user'

const state = {
    expenses_list:[],
    advances_list:[],
    market_costs_list:[],
    showLoading:false
}

// getters
// actions
const actions = {
        get_agency_list({
            commit
        }, param) {
            commit(types.AGENCY_SHOW_LOADING, param);
            get_list(param.curPage - 1, param.pageSize, param.role_id).then((res) => {
                    if (res.data.res_code === 1) {
                        commit(types.AGENCY_LIST_LOADED, res.data.msg);
                    }
                });
        },
        add_agency({
            commit
        }, param) {
            commit(types.AGENCY_SHOW_LOADING, param);
            get_list(param.curPage - 1, param.pageSize, param.role_id).then((res) => {
                    if (res.data.res_code === 1) {
                        commit(types.AGENCY_ADDED, {result:res.data.msg,data:param});
                        if(param._fn)
                            param._fn();
                    }
                });
        },
        update_agency({
            commit
        }, param) {
            commit(types.AGENCY_SHOW_LOADING, param);
            get_list(param.curPage - 1, param.pageSize, param.role_id).then((res) => {
                    if (res.data.res_code === 1) {
                        commit(types.AGENCY_UPDATED, param);
                        if(param._fn)
                            param._fn();
                    }
                });
        },
        delete_agency({
            commit
        }, param) {
            commit(types.AGENCY_SHOW_LOADING, param);
            get_list(param.curPage - 1, param.pageSize, param.role_id).then((res) => {
                    if (res.data.res_code === 1) {
                        commit(types.AGENCY_DELETED, param);
                        if(param._fn)
                            param._fn();
                    }
                });
        },
        update_agency_state({
            commit
        }, param)   {
            commit(types.AGENCY_SHOW_LOADING, param);
            get_list(param.curPage - 1, param.pageSize, param.role_id).then((res) => {
                    if (res.data.res_code === 1) {
                        commit(types.AGENCY_UPDATED, param);
                        if(param._fn)
                            param._fn();
                    }
                });
        }
    }
    // mutations
const mutations = {
    [types.AGENCY_SHOW_LOADING](state, param) {
        state.showLoading = true;
    },
    [types.AGENCY_LIST_LOADED](state, param) {
        state.agency_list = param.list;
        state.total = param.total;
        state.showLoading = false;
    },
    [types.AGENCY_ADDED](state, param) {
        state.agency_list.unshift({})
        state.showLoading = false;
    },
    [types.AGENCY_UPDATED](state, param) {
        for(var i=0;i<state.agency_list.length;i++)
        {
            if(state.agency_list[i].id === param.id)
            {

            }
        }
        state.showLoading = false;
    },
    [types.AGENCY_DELETED](state, param) {
        for(var i=0;i<state.agency_list.length;i++)
        {
            if(state.agency_list[i].id === param.id)
            {
                state.agency_list.splice(i,1);
                break;
            }
        }
        state.showLoading = false;
    }
}

export default {
    state,
    actions,
    mutations
}
